﻿Option Strict Off
Imports System.IO
Imports System.Net
Imports System.Drawing
Imports System.Drawing.Drawing2D

<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class frmBrowser

    Public WithEvents oDoc As HtmlDocument

    Private Sub frmBrowser_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        oDoc = Nothing
        WebBrowser1.Dispose()
    End Sub

    Private Sub frmBrowser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AppManager.CurrentBrowser = Me.WebBrowser1
        WebBrowser1.ObjectForScripting = Me
    End Sub

    Private Sub WebBrowser1_CanGoBackChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser1.CanGoBackChanged
        AppManager.MainForm.tsbBack.Enabled = WebBrowser1.CanGoBack
    End Sub

    Private Sub WebBrowser1_CanGoForwardChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser1.CanGoForwardChanged
        AppManager.MainForm.tsbForward.Enabled = WebBrowser1.CanGoForward
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        oDoc = WebBrowser1.Document
        If WebBrowser1.Url.ToString = "about:blank" Then
            If My.Settings.IsFormLoadingExceptURL = False Then
                AppManager.MainForm.Text = "Blank Page - BBrowser (Tabs Enabled)"
                Me.Text = "Blank Page"
            ElseIf My.Settings.IsFormLoadingExceptURL = True Then
                AppManager.MainForm.Text = "BBrowser (Tabs Enabled)"
                Me.Text = "Loading..."
            End If
        ElseIf My.Settings.IsFormLoadingExceptURL = True Then
            AppManager.MainForm.Text = "BBrowser (Tabs Enabled)"
            Me.Text = "Loading..."
        Else
            Me.Text = WebBrowser1.DocumentTitle
        End If
        AppManager.MainForm.webStatus.Text = WebBrowser1.StatusText
        AppManager.MainForm.CurrentUrl()
        My.Settings.BrowserHistory.Add(My.Settings.CurrentUrlForTabs)
    End Sub

    Private Function FixURL(ByVal sURL As String) As String
        sURL = sURL.Trim
        If Not sURL.ToLower().StartsWith("http://") _
        Then sURL = "http://" & sURL
        Return sURL
    End Function

    Private Sub WebBrowser1_DocumentTitleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser1.DocumentTitleChanged
        If WebBrowser1.Url.ToString = "about:blank" Then
            If My.Settings.IsFormLoadingExceptURL = False Then
                AppManager.MainForm.Text = "Blank Page - BBrowser (Tabs Enabled)"
                Me.Text = "Blank Page"
            ElseIf My.Settings.IsFormLoadingExceptURL = True Then
                AppManager.MainForm.Text = "BBrowser (Tabs Enabled)"
                Me.Text = "Loading..."
            End If
        ElseIf My.Settings.IsFormLoadingExceptURL = True Then
            AppManager.MainForm.Text = "BBrowser (Tabs Enabled)"
            Me.Text = "Loading..."
        Else
            Me.Text = WebBrowser1.DocumentTitle
            AppManager.MainForm.Text = WebBrowser1.DocumentTitle & " - BBrowser (Tabs Enabled)"
        End If
    End Sub

    Private Sub WebBrowser1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
        Try
            Me.Icon = AppManager.GetFavIcon(WebBrowser1.Document.Domain & "/favicon.ico")
        Catch ex As Exception
            Me.Icon = My.Resources.page161
        End Try

        Try
            AppManager.MainForm.WebBrowser1.TabPages(Me).Icon = Me.Icon
        Catch ex As Exception
        End Try

        If WebBrowser1.Url.ToString = "about:blank" Then
            If My.Settings.IsFormLoadingExceptURL = False Then
                AppManager.MainForm.Text = "Blank Page - BBrowser (Tabs Enabled)"
                Me.Text = "Blank Page"
            ElseIf My.Settings.IsFormLoadingExceptURL = True Then
                AppManager.MainForm.Text = "BBrowser (Tabs Enabled)"
                Me.Text = "Loading..."
            End If
        ElseIf My.Settings.IsFormLoadingExceptURL = True Then
            AppManager.MainForm.Text = "BBrowser (Tabs Enabled)"
            Me.Text = "Loading..."
        Else
            Me.Text = WebBrowser1.DocumentTitle
            AppManager.MainForm.Text = WebBrowser1.DocumentTitle & " - BBrowser (Tabs Enabled)"
        End If

        Try
            AppManager.MainForm.txtUrl.Text = WebBrowser1.Url.ToString
        Catch ex As Exception
        End Try

        With AppManager.MainForm.webProgress
            .Visible = False
            .Value = 0
        End With
    End Sub

    Private Sub WebBrowser1_ProgressChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged

        With AppManager.MainForm.webProgress
            .Visible = True
        End With

        AppManager.MainForm.webProgress.Maximum = e.MaximumProgress

        If Not e.CurrentProgress = -1 Then
            Try
                AppManager.MainForm.webProgress.Value = e.CurrentProgress
            Catch Ex As Exception
            End Try
        Else
            AppManager.MainForm.webProgress.Value = 0
        End If

        If AppManager.MainForm.webProgress.Value = 0 Then
            AppManager.MainForm.webProgress.Visible = False
        End If

        Try
            AppManager.MainForm.webStatus.Text = WebBrowser1.StatusText
        Catch ex As Exception
        End Try

        If My.Settings.IsFormOpenTabs = True Then
            Try
                AppManager.MainForm.txtUrl.Text = WebBrowser1.Url.ToString
            Catch Ex As Exception
            End Try
        End If

        Try
            If WebBrowser1.Url.ToString = "about:blank" Then
                If My.Settings.IsFormLoadingExceptURL = False Then
                    AppManager.MainForm.Text = "Blank Page - BBrowser (Tabs Enabled)"
                    Me.Text = "Blank Page"
                ElseIf My.Settings.IsFormLoadingExceptURL = True Then
                    AppManager.MainForm.Text = "BBrowser (Tabs Enabled)"
                    Me.Text = "Loading..."
                End If
            ElseIf My.Settings.IsFormLoadingExceptURL = True Then
                AppManager.MainForm.Text = "BBrowser (Tabs Enabled)"
                Me.Text = "Loading..."
            Else
                Me.Text = WebBrowser1.DocumentTitle
                AppManager.MainForm.Text = WebBrowser1.DocumentTitle & " - BBrowser (Tabs Enabled)"
            End If
        Catch Ex As Exception

        End Try
    End Sub

    Private Sub WebBrowser1_NavigatingExtended(ByVal sender As Object, ByVal e As exBrowser.WebBrowserNavigatingExtendedEventArgs) Handles WebBrowser1.NavigatingExtended

        'Is it a Media File?
        Dim strFile As String
        For Each strFile In My.Settings.MediaPlayerFileTypes
            If e.Url.EndsWith(strFile) Then
                e.Cancel = True
                Dim ofrm As New BBrowserMediaPlayer
                My.Settings.MediaURL = e.Url.ToString
                ofrm.Show()
                Exit Sub
            End If
        Next
    End Sub

    Private Sub WebBrowser1_NewWindowExtended(ByVal sender As Object, _
ByVal e As exBrowser.WebBrowserNewWindowExtendedEventArgs) Handles WebBrowser1.NewWindowExtended

        e.Cancel = True

        If My.Settings.OpenNewWindowInNewTab = False Then
            My.Settings.NewWindowCalled = True
            My.Settings.NewWindowURL = e.Url.ToString
            AppManager.MainForm.OpenNewWindow()
        ElseIf My.Settings.OpenNewWindowInNewTab = True Then
            Dim ofrm As New frmBrowser
            AppManager.AddTab(ofrm, e.Url.ToString)
        End If
    End Sub

#Region " Handleing Document Script Calls "
    'Since we are using the browser control, we have to handle javascript and other events raised by
    'the html document that target window.external such as add to favorites, organize favorites etc. 
    'We could implement this in the extended control itself, 
    'but for purposes of this demo, we will handle a couple common ones
    'in this region.
    '// Security note on Javascript handleing, you will probably want to check to make
    '//sure that the event was raised by a user click or show dialogs, not allow automatic running of
    '//these routines by using dialogs or other methods.
    '//Make sure your signatures match the common calls from the page.

    Public Sub AddFavorite(ByVal strURL As String, ByVal strTitle As String)
        WebBrowser1.AddToFavorites(Trim(strURL), Trim(strTitle))
    End Sub

    Public Sub OrganizeFavorites()
        WebBrowser1.ShowOrganizeFavorites()
    End Sub

    Public Sub ShowCalc()
        Process.Start("Calc.exe")
    End Sub

    Public Sub ShowSettings()
        Dim ofrm As New BBrowserSettings
        ofrm.ShowDialog(AppManager.MainForm)
    End Sub
#End Region

    Private Sub WebBrowser1_StatusTextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser1.StatusTextChanged
        Try
            AppManager.MainForm.webStatus.Text = WebBrowser1.StatusText
        Catch ex As Exception
        End Try
    End Sub
End Class